package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.UserService;
import com.dongdongshop.shiro.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Reference
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("register")
    @ResponseBody
    public dataResult register(TbUser t,String code){
        try {
            String code1 = (String) redisTemplate.opsForValue().get(t.getPhone());
            Integer i;
            if (code.equals(code1)){
                t.setSalt("da27c280");
                String pwd = t.getPassword();
                String password = ShiroUtils.encryptPassword("MD5",pwd,t.getSalt(),3);
                t.setCreated(new Date());
                t.setUpdated(new Date());
                t.setPassword(password);
                i = userService.register(t);
            }else{
                i = -1;
            }
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),i);
        }catch (Exception e){
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("login")
    public String login(TbUser t, Model model, HttpServletRequest request){
        /*
         * 使用shiro编写认证操作
         * */
        //1,获取subject
        Subject subject = SecurityUtils.getSubject();
        //2,封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(t.getUsername(),t.getPassword());
        //3,执行登录方法
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addAttribute("message","用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码不正确");
            return "login";
        }
        System.out.println(model.getAttribute("message"));
        HttpSession session = request.getSession();
        session.setAttribute("user",t);
        return "index";
    }
}
